package com.diu.domain.strategy.service.rule.filter.impl;

import com.diu.domain.strategy.model.entity.RuleActionEntity;
import com.diu.domain.strategy.model.entity.RuleMatterEntity;
import com.diu.domain.strategy.model.vo.RuleLogicCheckTypeVO;
import com.diu.domain.strategy.repository.IStrategyRepository;
import com.diu.domain.strategy.service.annotation.LogicStrategy;
import com.diu.domain.strategy.service.rule.filter.ILogicFilter;
import com.diu.domain.strategy.service.rule.filter.factory.DefaultLogicFactory;
import com.diu.types.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author #diu
 * @description #
 * @create #2025/10/10 16:45
 */
@Slf4j
@Component
@LogicStrategy(logicMode = DefaultLogicFactory.LogicModel.RULE_WIGHT)
public class RuleWeightLogicFilter implements ILogicFilter<RuleActionEntity.RaffleBeforeEntity> {
    @Resource
    private IStrategyRepository repository;

    private Long userScore = 4500L;
    @Override
    public RuleActionEntity<RuleActionEntity.RaffleBeforeEntity> filter(RuleMatterEntity ruleMatterEntity) {
        log.info("权重过滤器触发，userId:{} strategyId:{} ruleModel:{}", ruleMatterEntity.getUserId(), ruleMatterEntity.getStrategyId(), ruleMatterEntity.getRuleModel());
        String userId = ruleMatterEntity.getUserId();

        String ruleValue = repository.queryStrategyRuleValue(ruleMatterEntity.getStrategyId(), ruleMatterEntity.getAwardId(), ruleMatterEntity.getRuleModel());
        // 规则值示例：1000:10 3000:30 5000:50 7000:70 9000:90
        // 下面的方法不需要排序，但是需要遍历所有规则键值对
        if(ruleValue != null && !ruleValue.isEmpty()){
            String[] splitRuleValues = ruleValue.split(Constants.SPACE);
            String ruleWeightValueKey = null;
            for(String keyRule : splitRuleValues){
                if(userScore > Long.parseLong(keyRule.split(Constants.COLON)[0])){
                    ruleWeightValueKey = keyRule.split(Constants.COLON)[0];
                }
            }
            return RuleActionEntity.<RuleActionEntity.RaffleBeforeEntity>builder()
                    .code(RuleLogicCheckTypeVO.TAKE_OVER.getCode())
                    .info(RuleLogicCheckTypeVO.TAKE_OVER.getInfo())
                    .ruleModel(DefaultLogicFactory.LogicModel.RULE_WIGHT.getCode())
                    .data(RuleActionEntity.RaffleBeforeEntity.builder()
                            .strategyId(ruleMatterEntity.getStrategyId())
                            .ruleWeightValueKey(ruleWeightValueKey)
                            .beforeState("yes")
                            .build())
                    .build();
        }

        return RuleActionEntity.<RuleActionEntity.RaffleBeforeEntity>builder()
                .code(RuleLogicCheckTypeVO.ALLOW.getCode())
                .info(RuleLogicCheckTypeVO.ALLOW.getInfo())
                .ruleModel(DefaultLogicFactory.LogicModel.RULE_WIGHT.getCode())
                .data(RuleActionEntity.RaffleBeforeEntity.builder()
                        .strategyId(ruleMatterEntity.getStrategyId())
                        .beforeState("no")
                        .build())
                .build();
    }
}
